package com.example.levine.getauthcode;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import okhttp3.Call;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private LinearLayout ll_code;
    private TextView tv_code;
    private CheckBox cb_day;
    private EditText et_sum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cb_day = findViewById(R.id.cb_day);
        CheckBox cb_mou = findViewById(R.id.cb_mou);
        et_sum = findViewById(R.id.et_sum);
        TextView tv_ok = findViewById(R.id.tv_ok);
        tv_code = findViewById(R.id.tv_code);
        TextView tv_copy = findViewById(R.id.tv_copy);
        ll_code = findViewById(R.id.ll_code);
        ll_code.setVisibility(View.GONE);

        tv_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(et_sum.getText().toString().trim())) {
                    Toast.makeText(MainActivity.this, "请输入时限数量", Toast.LENGTH_SHORT).show();
                    return;
                }
                net();
            }
        });

        tv_copy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //复制
                ClipData clip = ClipData.newPlainText(System.currentTimeMillis() + "", tv_code.getText().toString().trim() + "");
                ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                clipboardManager.setPrimaryClip(clip);
                ClipData abc = clipboardManager.getPrimaryClip();
                ClipData.Item item = abc.getItemAt(0);
                T.showShort(MainActivity.this, "复制成功：" + item.getText().toString().trim());
                Log.e(TAG, "onClick: " + item.getText().toString().trim());
            }
        });
    }

    public void net() {
        final AlertDialog alertDialog = T.showDialog(this, "请稍等...", false)
                .setPositiveButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        OkGo.getInstance().cancelTag(TAG);
                    }
                }).create();
        alertDialog.show();
        OkGo.post("https://ra.jade-box.com/comments/code")
                .tag(TAG)
                .params("valid_type", cb_day.isChecked() ? "0" : "1")
                .params("valid_period_count", et_sum.getText().toString() + "")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        alertDialog.dismiss();
                        Entity entity;
                        try {
                            entity = new Gson().fromJson(s, Entity.class);
                        } catch (Exception e) {
                            return;
                        }
                        if (entity.getCode() == 201) {
                            ll_code.setVisibility(View.VISIBLE);
                            tv_code.setText(entity.getData().getAuth_code() + "");
                        } else {
                            ll_code.setVisibility(View.GONE);
                            T.showShort(MainActivity.this, "请求失败");
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        alertDialog.dismiss();
                        T.showShort(MainActivity.this, "请求失败");
                    }
                });
    }
}
